using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class EmbeddedResourceUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static byte[] GetEmbeddedResourceFileContent(Assembly assembly, string resourceName)
        {
            Stream fileStream = assembly.GetManifestResourceStream(assembly + "." + resourceName);
            if(fileStream!=null)
            {
                byte[] contents = new byte[fileStream.Length];
                fileStream.Read(contents, 0, contents.Length);
                fileStream.Close();
                return contents;    
            }
            else
            {
                throw new Exception(
                    string.Format("Unable to find resource {0} from assembly {1}", resourceName, assembly.GetName().Name));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static XmlDocument GetEmbeddedXmlFileContent(Assembly assembly, string resourceName)
        {
            XmlDocument xDoc=new XmlDocument();
            byte[] fileContents = EmbeddedResourceUtil.GetEmbeddedResourceFileContent(assembly, resourceName);
            UTF8Encoding encoding=new UTF8Encoding();
            string xmlContent = encoding.GetString(fileContents);
            xDoc.LoadXml(xmlContent);
            return xDoc;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static string GetEmbeddedTextFileContent(Assembly assembly, string resourceName)
        {
            byte[] fileContents = EmbeddedResourceUtil.GetEmbeddedResourceFileContent(assembly, resourceName);
            ASCIIEncoding encoding = new ASCIIEncoding();
            string asciiContent = encoding.GetString(fileContents);
            return asciiContent;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static Image GetEmbeddedImage(Assembly assembly, string resourceName)
        {
            byte[] fileContents = EmbeddedResourceUtil.GetEmbeddedResourceFileContent(assembly, resourceName);
            return ImageUtil.ToImage(fileContents);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static Icon GetEmbeddedIcon(Assembly assembly, string resourceName)
        {
            byte[] fileContents = EmbeddedResourceUtil.GetEmbeddedResourceFileContent(assembly, resourceName);
            return ImageUtil.ToIcon(fileContents);
        }
    }
}
